package com.wzh.yuvwater;

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Rect;
import android.graphics.drawable.BitmapDrawable;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;

import com.wzh.yuvwater.jni.YuvOsdUtils;
import com.wzh.yuvwater.utils.Logger1;
import com.wzh.yuvwater.utils.ThreadManager;

import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;

public class CreateNumberAct extends AppCompatActivity {

    private Paint p;
    private String TAG = "CreateNumberAct";

    public String[] numbers = new String[]{"0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "-", ":", " "};
    public String[] hanziChars = new String[]{"年","月","日"};

    public Map<String, byte[]> map = new HashMap<>();


    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.act_create_num);
        initView();
    }

    public static int getTextWidth(Paint paint, String str) {
        int iRet = 0;
        if (str != null && str.length() > 0) {
            int len = str.length();
            float[] widths = new float[len];
            paint.getTextWidths(str, widths);
            for (int j = 0; j < len; j++) {
                iRet += (int) Math.ceil(widths[j]);
            }
        }
        return iRet;
    }

    int defaultWidth = 16, defaultHeight = 32;
    int hanziWidth = 32;
    void generateYUVData(String[] numbers, boolean isHANZI){
        int w = defaultWidth, h = defaultHeight;
        if(isHANZI){
            w = hanziWidth;
        }

        for (int i = 0; i < numbers.length; i++) {
            String content = numbers[i];

            Rect r = new Rect();
            p.getTextBounds(content, 0, content.length(), r);

//                        p.setTypeface(tf);

            Bitmap srcBit = Bitmap.createBitmap(w, h, Bitmap.Config.ARGB_4444);
            Canvas canvas = new Canvas(srcBit);
            canvas.drawColor(Color.BLACK);

            int circleXY = (int) (w / 2);
            int circleRadius = (int) ((w * 0.5) / 2) + 20;
            //计算文本宽度
            int textWidth = getTextWidth(p,content);
            //计算baseline:垂直方向居中
            Paint.FontMetricsInt fontMetrics = p.getFontMetricsInt();
            int baseline = (h - fontMetrics.bottom + fontMetrics.top) / 2 - fontMetrics.top;
            Rect bounds = new Rect();
            p.getTextBounds(content, 0, content.length(), bounds);
            int textX = (circleXY-textWidth)/2+circleRadius-20;
//            canvas.drawText(content,textX,baseline,p);

            canvas.drawText(content, textX/*(w - r.width()) / 2*/, baseline/*(r.height() + h) / 2*/, p);

            runOnUiThread(() -> {
                ImageView iv = (ImageView) LayoutInflater.from(CreateNumberAct.this).inflate(R.layout.item_image_view, llRoot, false);
                iv.setImageBitmap(srcBit);
                llRoot.addView(iv);
            });

            byte[] nv12 = YuvOsdUtils.bitmapToGrayNV(srcBit, w, h);

            Logger1.i(TAG, "onClick: length=%s w*h=%s w=%s h=%s", nv12.length, w * h, w, h);
            StringBuilder sb = new StringBuilder();
            for (int j = 0; j < h; j++) {
                String line = "";
                for (int k = 0; k < w; k++) {
                    //黑色为0，其它为1
                    if (nv12[j * w + k] == 16) {
                        nv12[j * w + k] = 0;
                    } else {//其它为白色
                        nv12[j * w + k] = 1;
                    }
                    line += nv12[j * w + k];
                }
                sb.append(line + "\n");
            }
            Logger1.i(TAG, "onClick: \n%s", sb);


                       /* List<Byte> bytes=new ArrayList<>(100);
                        byte blackNum=-1;
                        for (int j = 0; j < nv12.length; j++) {
                            if(nv12[j]==16){
                                if(blackNum==-1){
                                    blackNum=0;
                                    bytes.add(nv12[j]);
                                }
                                blackNum++;
                                if(blackNum==Byte.MAX_VALUE){
                                    bytes.add(blackNum);
                                    blackNum=-1;
                                }
                            }else{
                                if(blackNum!=-1){
                                    bytes.add(blackNum);
                                    blackNum=-1;
                                }
                                bytes.add(nv12[j]);
                            }
                        }
                        if(blackNum!=-1){
                            bytes.add(blackNum);
                        }
                        Logger1.i(TAG, "onClick: c=%s newLen=%d %s",content,bytes.size(), bytes);
*/
            Logger1.i(TAG, "onClick: c=%s len=%d %s", content, nv12.length, Arrays.toString(nv12));

            map.put(content, nv12);

//                        Logger1.i(TAG, "initBase: len=%s==%s char=%s %s", w * h * 3 / 2, nv12.length, content, Arrays.toString(nv12));

        }
    }
    LinearLayout llRoot;
    public void initView() {
        /*LinearLayout*/ llRoot = (LinearLayout) findViewById(R.id.ll_root);
        if (p == null) {
            p = new Paint(Paint.ANTI_ALIAS_FLAG);
            p.setColor(Color.WHITE);
            p.setTextSize(30);
        }

        findViewById(R.id.bt).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                ThreadManager.getInstance().execute(() -> {
                    generateYUVData(numbers, false);
                    generateYUVData(hanziChars, true);

                    Bitmap bmp= BitmapFactory.decodeResource(getResources(), R.drawable.test);
                    bmp = Bitmap.createScaledBitmap(bmp, hanziWidth, defaultHeight, true);
                    byte[] nv12 = YuvOsdUtils.bitmapToNV12(bmp, hanziWidth, defaultHeight);
                    byte[] nv12_y1 = new byte[hanziWidth*defaultHeight/2];
                    byte[] nv12_y2 = new byte[hanziWidth*defaultHeight/2];
                    byte[] nv12_uv = new byte[hanziWidth*defaultHeight/2];
                    System.arraycopy(nv12, 0, nv12_y1, 0, hanziWidth*defaultHeight/2);
                    System.arraycopy(nv12, hanziWidth*defaultHeight/2, nv12_y2, 0, hanziWidth*defaultHeight/2);
                    System.arraycopy(nv12, hanziWidth*defaultHeight, nv12_uv, 0, hanziWidth*defaultHeight/2);

                    Logger1.i(TAG, "onClick: length=%s nv12_y1=%s", nv12_y1.length, Arrays.toString(nv12_y1));
                    Logger1.i(TAG, "onClick: length=%s nv12_y2=%s", nv12_y1.length, Arrays.toString(nv12_y2));
                    Logger1.i(TAG, "onClick: length=%s nv12_uv=%s", nv12_uv.length, Arrays.toString(nv12_uv));

                    StringBuilder sb = new StringBuilder();
                    for (int j = 0; j < defaultHeight; j++) {
                        String line = "";
                        for (int k = 0; k < hanziWidth; k++) {
                            //黑色为0，其它为1
                            if (nv12[j * hanziWidth + k] == 16) {
                                nv12[j * hanziWidth + k] = 0;
                            } else {//其它为白色
                                nv12[j * hanziWidth + k] = 1;
                            }
                            line += nv12[j * hanziWidth + k];
                        }
                        sb.append(line + "\n");
                    }
                    Logger1.i(TAG, "onClick: bitmap \n%s", sb);
                });

            }
        });

        findViewById(R.id.bt_save).setOnClickListener((View v) -> {

        });

    }
}
